package com.longjianghuasheng.logistics.business.freight

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.business.home.TypeResponse
import com.longjianghuasheng.logistics.databinding.FragmentShipmentServiceBinding

class ShipmentServiceFragment(private var types: List<TypeResponse>, var type: Int = 0) :
    BaseFragment<FreightViewModel, FragmentShipmentServiceBinding>() {


    //fragment集合
    val fragments by lazy {

        mutableListOf<Fragment>()
    }



    override fun layoutId(): Int = R.layout.fragment_shipment_service

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.initTabAndViewPager()
    }


    private fun FragmentShipmentServiceBinding.initTabAndViewPager() {
        fragments.clear()
        if (type != TYPE_MERCHANTS) {

            types.forEach {

                fragments.add(ServiceListFragment.newInstance(it.id ?: 0))
            }

        } else {

            fragments.add(DriverCooperationFragment())
            fragments.add(FleetOrEnterpriseFragment.newInstance(false))
            fragments.add(PersonalCooperationFragment())
            fragments.add(FleetOrEnterpriseFragment.newInstance(true))
        }

        viewPager.run {


            offscreenPageLimit = types.size

            adapter = ShipmentServiceAdapter(this@ShipmentServiceFragment)




            TabLayoutMediator(tabLayout, this) { tab, position ->
                tab.text = types[position].title


            }.attach()

        }


        tabLayout.getTabAt(0)
            ?.select()

    }

    override fun createObserver() {
        super.createObserver()
    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }

    companion object {
        private const val TAG = "ShipmentServiceFragment"

        const val TYPE_TRANSPORT = 1
        const val TYPE_SITE = 2
        const val TYPE_INSPECTION = 3
        const val TYPE_MERCHANTS = 4

    }

    inner class ShipmentServiceAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {


        override fun getItemCount(): Int = fragments.size

        override fun createFragment(position: Int): Fragment = fragments[position]

    }

}